package com.kye.express.business.tcl.home;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemTclBinding;
import com.kye.model.bean.tcl.ResponseTCLGoodsListBean;

import me.drakeet.multitype.ItemViewBinder;

/**
 * TCL首页 ItemViewHolder
 * <p>
 * Created by SkyYu on 2018/2/1
 */
public class TCLItemViewBinder extends ItemViewBinder<ResponseTCLGoodsListBean.DatasBean, TCLItemViewBinder.ViewHolder> {

    private OnGoodsItemListener onItemListener;

    TCLItemViewBinder(OnGoodsItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(inflater.inflate(R.layout.item_tcl, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ResponseTCLGoodsListBean.DatasBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemTclBinding binding;
        private ResponseTCLGoodsListBean.DatasBean item;
        private View view;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            view = itemView;
            RxView.clicks(binding.linearlayoutJdSearchResult)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(o -> {
                        if (onItemListener != null) {
                            onItemListener.onGoodsItemClicked(item);
                        }
                    });
        }

        public void bindData(ResponseTCLGoodsListBean.DatasBean item) {
            this.item = item;
            Glide.with(view.getContext())
                    .load(item.getUrlAddress())
                    .apply(new RequestOptions().error(R.drawable.error).placeholder(R.drawable.background_about_empty))
                    .into(binding.imageviewTcl);
            binding.setData(item);
        }
    }

    interface OnGoodsItemListener {
        void onGoodsItemClicked(ResponseTCLGoodsListBean.DatasBean item);
    }
}